import 'dart:async';

import 'package:bluetooth_protocols_parser_plugin/library.dart';
import 'package:inmotion_pro/custom_exception/api_exception.dart';
import 'package:inmotion_pro/custom_exception/lx_exception.dart';
import 'package:inmotion_pro/l10n/local.dart';
import 'package:inmotion_pro/service/log_service.dart';
import 'package:dio/dio.dart';
import 'package:flutter/widgets.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';

// 添加一个通用的错误处理函数
extension FutureErrorHandler<T> on Future<T> {
  Future<T> catchErrorHandler({Function(Exception)? onError}) {
    return catchError((err) {
      if (onError != null) onError(err);
      // 处理错误
      if (err is String && err.isNotEmpty) {
        // 处理异常
        Loading.showToast(err);
        LogService.to.error("Error: $err");
      } else if (err is TimeoutException) {
        // 处理异常（一般为蓝牙请求超时，因为网络请求超时的错误在http的错误拦截器中处理了）
        Loading.showToast(local.bluetooth_request_timeout);
        LogService.to.error("TimeoutException: ${err.message}");
      } else if (err is ApiException) {
        Loading.showToast(err.message);
        LogService.to.error("ApiException: ${err.message}");
      } else if (err is LxException) {
        // 处理异常
        Loading.showToast(err.message);
        LogService.to.error("LxException: ${err.message}");
      } else if (err is DioException) {
        // 已经在http的错误拦截器中处理了，此处不再处理
        debugPrint("Http的错误拦截器中处理了,此处不再处理DioException: ${err.message}");
      } else if (err is OtaException) {
        Loading.showToast(err.status.msg);
        LogService.to.error("OtaException: ${err.status.msg}");
      } else {
        // 处理异常
        String errMsg = err.toString().replaceAll("Exception:", "");
        Loading.showToast(errMsg);
        LogService.to.error("Exception: $errMsg");
      }
    });
  }
}

// 扩展OtaException，处理OtaException中的status，显示对应的错误信息
extension OtaUpgradeStatusExtension on OtaUpgradeStatus {
  String get msg {
    switch (this) {
      case OtaUpgradeStatus.started:
        return local.started;
      case OtaUpgradeStatus.startDownloading:
        return local.start_downloading;
      case OtaUpgradeStatus.downloadingFirmware:
        return local.downloading_firmware;
      case OtaUpgradeStatus.downloadFailed:
        return local.download_failed;
      case OtaUpgradeStatus.downloadSuccess:
        return local.download_success;
      case OtaUpgradeStatus.idle:
        return local.idle;
      case OtaUpgradeStatus.busy:
        return local.busy;
      case OtaUpgradeStatus.openFile:
        return local.open_file;
      case OtaUpgradeStatus.openFileFailed:
        return local.open_file_failed;
      case OtaUpgradeStatus.openFileSuccess:
        return local.open_file_success;
      case OtaUpgradeStatus.verifyFile:
        return local.verify_file;
      case OtaUpgradeStatus.verifyFileFailed:
        return local.verify_file_failed;
      case OtaUpgradeStatus.verifyFileSuccess:
        return local.verify_file_success;
      case OtaUpgradeStatus.readFile:
        return local.read_file;
      case OtaUpgradeStatus.readFileFailed:
        return local.read_file_failed;
      case OtaUpgradeStatus.readFileSuccess:
        return local.read_file_success;
      case OtaUpgradeStatus.writeFile:
        return local.write_file;
      case OtaUpgradeStatus.writeFileFailed:
        return local.write_file_failed;
      case OtaUpgradeStatus.writeFileSuccess:
        return local.write_file_success;
      case OtaUpgradeStatus.closeFileSuccess:
        return local.close_file_success;
      case OtaUpgradeStatus.closeFileFailed:
        return local.close_file_failed;
      case OtaUpgradeStatus.preparingTransfer:
        return local.preparing_transfer;
      case OtaUpgradeStatus.transferring:
        return local.transferring;
      case OtaUpgradeStatus.transferPaused:
        return local.transfer_paused;
      case OtaUpgradeStatus.transferFailed:
        return local.transfer_failed;
      case OtaUpgradeStatus.transferSuccess:
        return local.transfer_success;
      case OtaUpgradeStatus.waitingUserAction:
        return local.waiting_user_action;
      case OtaUpgradeStatus.upgrading:
        return local.upgrading;
      case OtaUpgradeStatus.upgradeSuccess:
        return local.upgrade_success;
      case OtaUpgradeStatus.upgradeFailed:
        return local.upgrade_failed;
      case OtaUpgradeStatus.cancelled:
        return local.cancel;
    }
  }
}
